#define _CRT_SECURE_NO_WARNINGS 1

#include"SList.h"
#include <assert.h>

void SLTPrint(SLNode* phead)
{
	//assert(phead);

	SLNode* cur = phead;
	while (cur != NULL)
	{
		printf("%d->", cur->v);
		cur = cur->next;
	}
	printf("NULL");
	printf("\n");
}

SLNode* CreateNode(SLNDataType x)
{
	SLNode* newnode = (SLNode*)malloc(sizeof(SLNode));

	if (newnode == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}

	newnode->v = x;
	newnode->next = NULL;
	return newnode;
}

void SLTPushback(SLNode** pphead, SLNDataType x)
{
	assert(pphead);
	//assert(*pphead);
	


	SLNode* newnode = CreateNode(x);

	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		SLNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		SLNode* newnode = CreateNode(x);
		tail->next = newnode;
	}
}
void SLTPushfront(SLNode** pphead, SLNDataType x)
{
	assert(pphead);

	SLNode* newnode = CreateNode(x);

	newnode->next = *pphead;
	*pphead = newnode;

}

void SLTPopback(SLNode** pphead)
{
	assert(pphead);
	assert(*pphead);

	/*if (*pphead == NULL)
	{
		return;
	}*/

	//assert(*pphead);

	if ((*pphead)->next == NULL)
	{
		free(*pphead);
		*pphead = NULL;
	}
	else
	{
	//	SLNode* prev = NULL;
	//	SLNode* tail = *pphead;
	//	while (tail->next != NULL)
	//	{
	//		prev = tail;
	//		tail = tail->next;
	//	}
	//	free(tail);
	//	tail = NULL;
	//	prev->next = NULL;

		SLNode* tail = *pphead;
		while (tail->next->next != NULL)
		{
			tail = tail->next;
		}

		free(tail->next);
		tail->next = NULL;
	}

}
void SLTPopfront(SLNode** pphead)
{
	assert(pphead);

	assert(*pphead);

	SLNode* tmp = (*pphead);
	*pphead = (*pphead)->next;
	free(tmp);

}

SLNode* SLTFind(SLNode* pphead, SLNDataType x)
{
	assert(pphead);

	SLNode* cur = pphead;
	while (cur)
	{
		if (cur->v == x)
		{
			return cur;
		}
		else
		{
			cur = cur->next;
		}
	}
	return NULL;
}

void SLTInsert(SLNode** pphead, SLNode* pos, SLNDataType x)
{
	assert(pphead);
	//assert(*pphead);

	assert((!pos && !(*pphead)) ||(pos && *pphead));
	
	if (*pphead == pos)
	{
		SLTPushfront(pphead, x);
	}
	else
	{
		SLNode* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		SLNode* newnode = CreateNode(x);
		prev->next = newnode;
		newnode->next = pos;

	}
}

void SLTErase(SLNode** pphead, SLNode* pos)
{
	assert(pphead);
	assert(*pphead);
	assert(pos);

	if (*pphead == pos)
	{
		SLTPopfront(pphead);
	}
	else
	{
		SLNode* prev = *pphead;
		while (prev->next != pos)
		{
			prev = prev->next;
		}
		prev->next = pos->next;
		free(pos);
		pos = NULL;
	}
}

void SLTInsertAfter(SLNode* pos, SLNDataType x)
{
	assert(pos);
	SLNode* newnode = CreateNode(x);

	newnode->next = pos->next;

	pos->next = newnode;
}
void SLTEraseAfter(SLNode* pos)
{
	assert(pos);
	assert(pos->next);

	SLNode* tmp = pos->next;
	pos->next = pos->next->next;

	free(tmp);
	tmp = NULL;
}



void SLTDestory(SLNode** pphead)
{
	assert(pphead);

	SLNode* cur = *pphead;
	while (cur)
	{
		SLNode* next = cur->next;
		free(cur);
		cur = next;
	}
	*pphead = NULL;
}